//
//  MYShopGoodsInsuranceTestifyVC.m
//  MuYing
//
//  Created by tbb on 2019/7/31.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYShopGoodsInsuranceTestifyVC.h"
#import "YQMYBaseNavigationController.h"
#import "YQImagePickerBaseVC.h"
#import "BRPickerView.h"
@interface MYShopGoodsInsuranceTestifyVC ()<MYShopGoodsInsuranceTestifyImageCellDelegate>
@property (nonatomic, strong) NSMutableArray * imageDataArr;
@property (nonatomic, copy) NSString * dateStr;
@end

@implementation MYShopGoodsInsuranceTestifyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"上传凭证");
    self.rightItemTitle = kYQLS(@"提交");
    [self.imageDataArr addObject:YQIMG(@"MY_Add_Imaeg_icon")];

}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self _setNavShadow];
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self _clearNavShadow];
}

-(void)rightItemAction:(UIButton *)sender {
    
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopGoodsInsuranceTestifyBankcardNumCell");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopGoodsInsuranceTestifyDateCell");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopGoodsInsuranceTestifyExplainCell");
    kYQREGISTERTABLEVIEWCELL([MYShopGoodsInsuranceTestifyImageCell class])
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MYShopGoodsInsuranceTestifyBankcardNumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopGoodsInsuranceTestifyBankcardNumCell"];
        cell.numLb.text = [NSString stringWithFormat:@"%@：6217 0030 9000 5643 123",kYQLS(@"建设银行对公账号")];
        return cell;
    }else if (indexPath.section == 1) {
        MYShopGoodsInsuranceTestifyDateCell *dateCell = [tableView dequeueReusableCellWithIdentifier:@"MYShopGoodsInsuranceTestifyDateCell"];
        return dateCell;
    }else if (indexPath.section == 3) {
        MYShopGoodsInsuranceTestifyExplainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopGoodsInsuranceTestifyExplainCell"];
        return cell;
    }
    MYShopGoodsInsuranceTestifyImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopGoodsInsuranceTestifyImageCell"];
    cell.delegate = self;
    cell.imageArr = self.imageDataArr;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 53;
    }else if (indexPath.section == 1) {
        return 58;
    }else if (indexPath.section == 3) {
        return 51;
    }
    return 123;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        MYShopGoodsInsuranceTestifyDateCell *cell = (MYShopGoodsInsuranceTestifyDateCell *)[tableView cellForRowAtIndexPath:indexPath];
        NSDate *minDate = [NSDate br_setYear:2019 month:06 day:31];
        NSString *dateStr = [NSDate currentDateStrFormatter:@"yyyy-MM-dd"];
        [BRDatePickerView showDatePickerWithTitle:@"" dateType:(BRDatePickerModeYMD) defaultSelValue:dateStr minDate:minDate maxDate:[NSDate date] isAutoSelect:NO themeColor:kAPPThemeColor resultBlock:^(NSString *selectValue) {
            self.dateStr = selectValue;
            cell.dateLb.text = self.dateStr;
        } cancelBlock:^{
            
        }];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
#pragma makr -- lazy
-(NSMutableArray *)imageDataArr {
    if (!_imageDataArr) {
        _imageDataArr = [NSMutableArray array];
    }
    return _imageDataArr;
}
#pragma makr -- lazy end

#pragma mark -- MYShopGoodsInsuranceTestifyImageCellDelegate
- (void)didSelectItemAtImageAction:(NSIndexPath *)indexPath {
    
    YQImagePickerBaseVC *imagePickerVc = [[YQImagePickerBaseVC alloc] initWithMaxImagesCount:6 delegate:nil];
    imagePickerVc.maxImagesCount = 6 - self.imageDataArr.count + 1;
    
    ws(self);
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        if (photos.count > 0) {
            [weakself.imageDataArr removeObject:YQIMG(@"MY_Add_Imaeg_icon")];
            [weakself.imageDataArr addObjectsFromArray:photos];
            if (weakself.imageDataArr.count < 6) {
                [self.imageDataArr addObject:YQIMG(@"MY_Add_Imaeg_icon")];
            }
            [weakself.listTableView reloadData];
        }
        
        
    }];
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}

@end
